package com.example.demo.cat;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.config.KafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.listener.ConcurrentMessageListenerContainer;
import org.springframework.kafka.listener.ContainerProperties;
import org.springframework.kafka.listener.KafkaMessageListenerContainer;
import org.springframework.kafka.listener.MessageListener;

import java.util.HashMap;
import java.util.Map;

@Configuration
@EnableKafka
public class KafkaConsumerFactory {

//    @Value("${kafka.consumer.servers}")
//    private String servers = "10.0.104.91:9092,10.0.104.91:9093,10.0.104.91:9094";
    private String servers = "10.2.112.60:9092";
//    @Value("${kafka.consumer.enable.auto.commit}")
    private boolean enableAutoCommit = false;
//    @Value("${kafka.consumer.session.timeout}")
    private String sessionTimeout = "6000";
//    @Value("${kafka.consumer.auto.commit.interval}")
    private String autoCommitInterval = "100";
//    @Value("${kafka.consumer.group.id}")
    private String groupId = "group.test";
//    @Value("${kafka.consumer.auto.offset.reset}")
    private String autoOffsetReset = "latest";
//    @Value("${kafka.consumer.concurrency}")
    private int concurrency = 10;


//    @Bean
//    public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> kafkaListenerContainerFactory() {
//        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
//        factory.setConsumerFactory(consumerFactory());
//        factory.setConcurrency(concurrency);
//        factory.getContainerProperties().setPollTimeout(1500);
//        return factory;
//    }

    public ConsumerFactory<String, String> consumerFactory() {
        return new DefaultKafkaConsumerFactory<>(consumerConfigs());
    }


    public Map<String, Object> consumerConfigs() {
        Map<String, Object> propsMap = new HashMap<>();
        propsMap.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, servers);
        propsMap.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, enableAutoCommit);
        propsMap.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, autoCommitInterval);
        propsMap.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, sessionTimeout);
        propsMap.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        propsMap.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        propsMap.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
        propsMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, autoOffsetReset);
        return propsMap;
    }
    @Bean
    public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        factory.setConcurrency(concurrency);
        factory.setAutoStartup(false);

        return factory;
    }

    public KafkaMessageListenerContainer createListenerContainer(String groupId,String... topic) {
        ContainerProperties properties = new ContainerProperties(topic);

        properties.setGroupId(groupId);

        properties.setMessageListener(new SingleAckMessageListener());

        return new KafkaMessageListenerContainer(consumerFactory(), properties);
    }


//    @Bean
//    public Listener listener() {
//        return new Listener();
//    }

}
